import ViewMgr from './ViewMgr';
import { UICommandKey } from '../define/UICommand';
import { tool } from '../tool/UITool';
import { ItemResultId, SceneName } from '../define/GameDefine';
import LoadingUI from '../view/loading/LoadingUI';
import RootView from '../view/RootView';
import GameSet from '../define/GameSet';

export default class SceneMgr {
  private static _inst: SceneMgr;
  public static get inst(): SceneMgr {
    if (!this._inst) {
      this._inst = new SceneMgr();
    }
    return this._inst;
  }

  public sceneName: SceneName;
  public scene: Node;

  public init(): void {
    this.loadPrefab();
  }

  public loadingPanel: LoadingUI = null;
  public root: RootView;
  public loadPrefab(initCallback?: Function) {
    cc.loader.loadResDir('prefab/common', (err, prefab) => {
      //cc.log("prefab/loadingPanel" + cc.instantiate(prefab).getComponent("LoadingPanel"));
      this.root = cc.instantiate(cc.loader.getRes('prefab/common/RootLayer')).getComponent('RootView');
      cc.game.addPersistRootNode(this.root.node);
      this.root.init();
      this.root.node.active = true;
      this.root.node.x = GameSet.designWidth / 2;
      this.root.node.y = GameSet.designHeight / 2;
    });
    cc.loader.loadResDir('loading', (err, prefab) => {
      //cc.log("prefab/loadingPanel" + cc.instantiate(prefab).getComponent("LoadingPanel"));
      this.loadingPanel = cc.instantiate(cc.loader.getRes('loading/LoadingUI')).getComponent('LoadingUI');
      cc.game.addPersistRootNode(this.loadingPanel.node);
      this.loadingPanel.node.active = false;
      this.loadingPanel.node.x = GameSet.designWidth / 2;
      this.loadingPanel.node.y = GameSet.designHeight / 2;
    });
    //if (initCallback) initCallback.call(this._this);
  }

  updatePos(x:number,y:number):void{
      this.root.node.x = x;
      this.root.node.y = y;
  }

  public loadAllPrefab(callback?: Function, callObj?: any): void {
    cc.loader.loadResDir(
      'prefab',
      (now, total, item) => {
        let proNum: number = now / total;
        this.cacheScenePro(proNum);
      },
      (err, prefab) => {
        if (callback) callback.call(callObj);
        tool.arrangePrefab(prefab);
      },
    );
  }

  public changeSceneing: boolean = false;
  public changeScene(sceneName: SceneName) {
    if (this.changeSceneing) return;
    //先抛出个事件 回收下资源
    ViewMgr.ins.emit(UICommandKey.onChangeScene);
    ViewMgr.ins.closeAll();
    //======
    this.sceneName = sceneName;
    this.changeSceneing = true;

    this.loadingPanel.node.active = true;
    this.loadingPanel.setPro(0);
    this.loadingPanel.node.opacity = 0;
    var seq = cc.sequence(
      cc.fadeIn(0.1),
      cc.callFunc(() => {
        this.cacheScene(sceneName);
        this.cachePrefab();
      }),
    );
    this.loadingPanel.node.runAction(seq);
  }

  public cachePrefab() {
    //预加载文件
    // let that = this;
    // cc.loader.loadRes('config/preLoad', function (err, jsonAsset) {
    //     if (err) {
    //         cc.log(err);
    //         return;
    //     }
    //     var data = jsonAsset.json;
    //     that.preLoadPrefab(data, 0);
    // });
  }

  public cacheScene(sceneName: string) {
    cc.director.preloadScene(
      sceneName,
      (completedCount: number, totalCount: number, item: any) => {
        this.cacheScenePro(completedCount / totalCount);
      },
      () => {
        this.loadScene();
      },
    );
    // cc.director.preloadScene(sceneName);
  }

  public cacheScenePro(pro: number) {
    //cc.log(pro);
    this.loadingPanel.setPro(pro);
  }

  public loadScene() {
    cc.director.loadScene(this.sceneName, () => {
      this.loadingPanel.setPro(1);
      this.loadingPanel.node.runAction(
        cc.sequence(
          cc.fadeOut(0.5),
          cc.callFunc(() => {
            this.loadingPanel.node.active = false;
            this.changeSceneing = false;
          }),
        ),
      );
    });
  }
}
